home *** CD-ROM | disk | FTP | other *** search
/ American Osteopathic Ass…tion Yearbook 2005 & 2006 / American Osteopathic Association Yearbook 2005 & 2006.iso / mac / app / encodings / __init__.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2005-02-02  |  4.2 KB  |  123 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.3)
  3.  
  4. ''' Standard "encodings" Package
  5.  
  6.     Standard Python encoding modules are stored in this package
  7.     directory.
  8.  
  9.     Codec modules must have names corresponding to normalized encoding
  10.     names as defined in the normalize_encoding() function below, e.g.
  11.     \'utf-8\' must be implemented by the module \'utf_8.py\'.
  12.  
  13.     Each codec module must export the following interface:
  14.  
  15.     * getregentry() -> (encoder, decoder, stream_reader, stream_writer)
  16.     The getregentry() API must return callable objects which adhere to
  17.     the Python Codec Interface Standard.
  18.  
  19.     In addition, a module may optionally also define the following
  20.     APIs which are then used by the package\'s codec search function:
  21.  
  22.     * getaliases() -> sequence of encoding name strings to use as aliases
  23.  
  24.     Alias names returned by getaliases() must be normalized encoding
  25.     names as defined by normalize_encoding().
  26.  
  27. Written by Marc-Andre Lemburg (mal@lemburg.com).
  28.  
  29. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  30.  
  31. '''
  32. import codecs
  33. import exceptions
  34. import types
  35. _cache = { }
  36. _unknown = '--unknown--'
  37. _import_tail = [
  38.     '*']
  39. _norm_encoding_map = '                                              . 0123456789       ABCDEFGHIJKLMNOPQRSTUVWXYZ      abcdefghijklmnopqrstuvwxyz                                                                                                                                     '
  40.  
  41. class CodecRegistryError(exceptions.LookupError, exceptions.SystemError):
  42.     pass
  43.  
  44.  
  45. def normalize_encoding(encoding):
  46.     """ Normalize an encoding name.
  47.  
  48.         Normalization works as follows: all non-alphanumeric
  49.         characters except the dot used for Python package names are
  50.         collapsed and replaced with a single underscore, e.g. '  -;#'
  51.         becomes '_'. Leading and trailing underscores are removed.
  52.  
  53.         Note that encoding names should be ASCII only; if they do use
  54.         non-ASCII characters, these must be Latin-1 compatible.
  55.  
  56.     """
  57.     if type(encoding) is types.UnicodeType:
  58.         encoding = encoding.encode('latin-1')
  59.     
  60.     return '_'.join(encoding.translate(_norm_encoding_map).split())
  61.  
  62.  
  63. def search_function(encoding):
  64.     entry = _cache.get(encoding, _unknown)
  65.     if entry is not _unknown:
  66.         return entry
  67.     
  68.     modname = normalize_encoding(encoding)
  69.     
  70.     try:
  71.         mod = __import__('encodings.' + modname, globals(), locals(), _import_tail)
  72.     except ImportError:
  73.         import aliases
  74.         if not aliases.aliases.get(modname) and aliases.aliases.get(modname.replace('.', '_')):
  75.             pass
  76.         modname = modname
  77.         
  78.         try:
  79.             mod = __import__(modname, globals(), locals(), _import_tail)
  80.         except ImportError:
  81.             mod = None
  82.         except:
  83.             None<EXCEPTION MATCH>ImportError
  84.         
  85.  
  86.         None<EXCEPTION MATCH>ImportError
  87.  
  88.     
  89.     try:
  90.         getregentry = mod.getregentry
  91.     except AttributeError:
  92.         mod = None
  93.  
  94.     if mod is None:
  95.         _cache[encoding] = None
  96.         return None
  97.     
  98.     entry = tuple(getregentry())
  99.     if len(entry) != 4:
  100.         raise CodecRegistryError, 'module "%s" (%s) failed to register' % (mod.__name__, mod.__file__)
  101.     
  102.     for obj in entry:
  103.         if not callable(obj):
  104.             raise CodecRegistryError, 'incompatible codecs in module "%s" (%s)' % (mod.__name__, mod.__file__)
  105.             continue
  106.     
  107.     _cache[encoding] = entry
  108.     
  109.     try:
  110.         codecaliases = mod.getaliases()
  111.     except AttributeError:
  112.         pass
  113.  
  114.     import aliases
  115.     for alias in codecaliases:
  116.         if not aliases.aliases.has_key(alias):
  117.             aliases.aliases[alias] = modname
  118.             continue
  119.     
  120.     return entry
  121.  
  122. codecs.register(search_function)
  123.